import { createRouter, createWebHistory } from 'vue-router'
import Login from '/src/components/views/Login.vue'
import Main from '/src/components/views/Main.vue'
import Register from "@/components/views/Register.vue"
import MainContent from "@/components/MainContent.vue";

const routes = [
    {
        path: '/',
        redirect: '/login'
    },
    {
        path: '/announcement/detail',
        name: 'AnnDetailReply',
        component: import('/src/components/message/AnnDetailReply.vue'),
        // props: true
    },
    {
        path: '/myMessage',
        name: 'MyMessage',
        component: import('/src/components/message/MyMessage.vue'),
    },
    {
        path: '/myAnnouncement',
        name: 'MyAnnouncement',
        component: import('/src/components/message/MyAnnouncement.vue'),
    },
    {
        path: '/getProfile',
        name: 'GetProfile',
        component: import('/src/components/views/GetProfile.vue'),
    },
    {
        path: '/resetPassword',
        name: 'ResetPassword',
        component: import('/src/components/views/ResetPassword.vue'),
    },
    {
        path: '/login',
        name: 'Login',
        component: Login
    },
    {
        path: '/newOrEditAnnouncement',
        name: 'NewOrEditAnnouncement',
        component: import('/src/components/message/NewOrEditAnnouncement.vue'),
    },
    {
        path: '/register',
        name: 'Register',
        component: Register
    },
    // {
    //     path: '/system/systemAnnouncement',
    //     name: 'SystemAnnouncement',
    //     component: import('/src/components/SystemAnnouncement.vue'),
    // },
    {
        path: '/main',
        name: 'Main',
        component: Main,
        meta: { requiresAuth: true },
        children: [
            // 主内容区的子路由，SystemAnnouncement作为其中一个页面
            {
                path: 'system/systemAnnouncement',
                name: 'SystemAnnouncement',
                component: () => import('/src/components/message/SystemAnnouncement.vue')
            },
        ]
    },
]

const router = createRouter({
    history: createWebHistory(),
    routes
})

router.beforeEach((to, from, next) => {
    const isAuthenticated = !!localStorage.getItem('token')

    if (isAuthenticated && (to.name === 'Login' || to.name === 'Register')) {
        next('/main')
    } else if (to.meta.requiresAuth && !isAuthenticated) {
        next('/login')
    } else {
        next()
    }
})

export default router